package com.hetao.assist.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.hetao.assist.dal.mapper.StockMapper;
import com.hetao.assist.dubbo.api.DemoService;
import com.hetao.assist.model.JsonPage;
import com.hetao.assist.model.ResponseObject;
import com.hetao.assist.model.Stock;
import com.hetao.assist.service.dal.StockService;
import com.hetao.assist.service.mq.RabbitMqConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
public class StockController extends BaseController{

    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private StockMapper stockMapper;

    @Autowired
    private StockService stockService;

    @Reference(check = false, timeout = 5000)
    private DemoService demoService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/hello")
    @ResponseBody
    public ResponseObject<List<Stock>> hello() {
        Stock stock = new Stock();
        stock.setDate("2007-01-22");
        List<Stock> stockList = stockMapper.list(stock);
        return new ResponseObject<>(stockList);
    }

    public static int pageNum = 0;

    @GetMapping("/date")
    @ResponseBody
    public JsonPage<Stock> date() {
        Page<Stock> page = stockService.pageByDate("2007-01-22", pageNum, 20);
        pageNum++;
        return convertPage(page);
    }

    @GetMapping("/demo")
    @ResponseBody
    public ResponseObject<String> demo() {

        return new ResponseObject<>(demoService.sayHello("demo"));

    }

    @GetMapping("/mq")
    @ResponseBody
    public ResponseObject<String> mq() {
//        rabbitTemplate.convertAndSend(RabbitMqConfig.FANOUT_SMS_QUEUE, "wo yao fa xiaoxi");
//        rabbitTemplate.convertAndSend("fanoutExchange1", "", "wo yao fa xiaoxi");
        Stock stock = new Stock();
        stock.setCode("1111");
        rabbitTemplate.convertAndSend("test-exchange", "friendInfo", stock);
        return new ResponseObject<>("ok");

    }


    @GetMapping("/redis")
    @ResponseBody
    public ResponseObject<String> redis() {
        String key = "hello.value";
        redisTemplate.opsForValue().set(key, "nihao", 30, TimeUnit.SECONDS);
        String value = redisTemplate.opsForValue().get(key);
        return new ResponseObject<>(value);

    }

}
